<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>day241128-chat-demo</title>

    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        html, body {
            width: 100%;
            height: 100%;
            background-color: #f0f0f5;
            font-family: Arial, sans-serif;
            display: flex;
            align-items: stretch; /* 让左右子元素拉伸占满高度 */
        }

        /* 整体容器，左右布局 */
        .main-container {
            display: flex;
            width: 100%;
            height: 100vh;
        }

        /* 左边选择框容器样式 */
        .left-panel {
            width: 200px;
            background-color: #e0e0e0;
            border-right: 1px solid #ccc;
            padding: 20px;
            display: flex;
            flex-direction: column;
        }

        /* 左边选择框内头像样式 */
        .left-panel img {
            border-radius: 50%;
            width: 48px;
            height: 48px;
            border: 1px solid #ccc;
        }

        /* 左边选择框内按钮样式 */
        .left-panel button {
            margin-bottom: 10px;
            padding: 8px 16px;
            border: none;
            border-radius: 4px;
            background-color: #007bff;
            color: white;
            cursor: pointer;
            transition: background-color 0.2s ease;
        }

        /* 左边选择框内按钮鼠标悬停样式 */
        .left-panel button:hover {
            background-color: #0056b3;
        }

        /* 聊天容器样式，占据剩余宽度且高度占满 */
        .chat-container {
            flex-grow: 1;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            padding: 20px;
            height: 100%;
        }

        .chat-history {
            border: 1px solid #d84315;
            flex-grow: 1;
            padding: 16px;
            overflow-y: auto;
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }

        .input-container {
            border: 1px solid #2e7d32;
            padding: 16px;
            display: flex;
            justify-content: space-between;
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }

        #message-input {
            flex-grow: 1;
            padding: 8px;
            border: 1px solid #ccc;
            border-radius: 4px;
            font-size: 16px;
            color: #333;
        }

        #send-button {
            margin-left: 8px;
            padding: 8px 16px;
            background-color: #2e7d32;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.2s ease;
        }

        #send-button:hover {
            background-color: #4caf50;
        }

        .message {
            margin-bottom: 15px;
            display: flex;
        }

        .message-content {
            padding: 10px 15px;
            border-radius: 15px;
            max-width: 70%;
            word-wrap: break-word;
            font-size: 14px;
            line-height: 1.4;
        }

        .user-message {
            justify-content: flex-end;
        }

        .ai-message {
            justify-content: flex-start;
        }

        .user-message.message-content {
            background: #03a9f4;
            color: white;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15);
        }

        .ai-message.message-content {
            background: #eceff1;
            color: black;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }
        /* 加粗 hr 样式及增加上下间隔 */
        hr {
            border: none;
            height: 3px;
            background-color: #000;
            margin-top: 20px; /* 设置上外边距 */
            margin-bottom: 20px; /* 设置下外边距 */
        }
    </style>
</head>

<body>
<div class="main-container">
    <!-- 左边选择框部分 -->
    <div class="left-panel">
        <!-- 头像区域 -->
        <div class="flex items-center justify-between mt-12 ml-16 mr-10">
            <img src="../sight.jpg" alt="头像描述" class="rounded-full w-12 h-12 border border-gray-300">
        </div>

        <hr>

        <!-- 按钮区域 -->
        <button onclick="newConversation()">新对话</button>
        <button onclick="alert('这是按钮 2 的功能示例，可替换为其他功能代码')">按钮 2</button>
        <button onclick="alert('这是按钮 3 的功能示例，可替换为其他功能代码')">按钮 3</button>
    </div>
    <!-- 右边聊天部分 -->
    <div class="chat-container">
        <div class="chat-history" id="chat-history">
            <!-- 聊天记录将显示在这里 -->
        </div>
        <div class="input-container">
            <input type="text" id="message-input" placeholder="输入消息...">
            <button id="send-button">发送</button>
        </div>
    </div>
</div>

<script src="jquery.min.js"></script>

<script>
    const chatHistory = document.getElementById('chat-history');
    const messageInput = document.getElementById('message-input');
    const sendButton = document.getElementById('send-button');
    sendButton.addEventListener('click', send);
    //回车发送
    messageInput.addEventListener('keypress', (e) => {
        if (e.key === 'Enter') {
            send();
        }
    });

    function send() {
        // 去除前后空格trim
        const message = messageInput.value.trim();
        // alert(message)弹窗
        if (message) {
            addMessage(message);
            messageInput.value = '';
            getStreamResponse(message);
        }
    }

    function getResponseFromServer(message) {
        $.get("/ai/chat", { message: message }, function (resp) {
            addMessage(resp, true);
        });
    }

    function addMessage(content, isAI = false) {
        const messageDiv = document.createElement('div');
        messageDiv.className = `message ${isAI? 'ai-message' : 'user-message'}`;

        const messageContent = document.createElement('div');
        messageContent.className = 'message-content';
        messageContent.textContent = content;

        messageDiv.appendChild(messageContent);
        chatHistory.appendChild(messageDiv);
        // 滚动到最新消息
        chatHistory.scrollTop = chatHistory.scrollHeight;

        return messageContent;

    }

    // 创建AI消息占位符
    function getStreamResponse(message) {
        const aiMessageContent = addMessage('', true);

        // 创建EventSource连接
        const eventSource = new EventSource(`/ai/chat-stream?prompt=${encodeURIComponent(message)}`);

        // 用于累积接收到的内容
        let accumulatedContent = '';

        // 处理接收到的消息
        eventSource.onmessage = function (event) {
            accumulatedContent += event.data;
            aiMessageContent.textContent = accumulatedContent;
            chatHistory.scrollTop = chatHistory.scrollHeight;
        };

        // 处理错误
        eventSource.onerror = function (error) {
            eventSource.close;
            if (accumulatedContent === '') {
                aiMessageContent.textContent = '抱歉，发生错误了';
            }
        };
    }

    function newConversation() {
        // 清空聊天记录
        const chatHistory = document.getElementById('chat-history');
        while (chatHistory.firstChild) {
            chatHistory.removeChild(chatHistory.firstChild);
        }

        // 可在此添加其他新对话相关的初始化操作，比如重置某些状态变量等
        // 目前示例中暂未涉及其他额外初始化内容，仅完成聊天记录清空
    }
</script>

</body>

</html>